import pymysql
# from DBUtils.SimplePooledDB import PooledDB
from DBUtils.PooledDB import PooledDB
from util.selenium.webdriver.common.by import By

from settings import online_config, dev_config, check_map


class g(object):

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp," +
                  "image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "max-age=0",
        "Content-Type": "application/x-www-form-urlencoded",
        "Host": "gqqy.chinatorch.org.cn",
        'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
        'sec-ch-ua-mobile': '?0',
        'Sec-Fetch-Dest': 'iframe',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        "Upgrade-Insecure-Requests": "1",
        "Connection": "Close"
        # "Connection": "keep-alive"
    }

    # 存储 cookie 字符串
    cookie = {
        "JSESSIONID": "",
        "insert_cookie": "",
        "AD_VALUE": ""
    }
    # 存储页面中的 token 值
    token = ""
    # 当前ip标记数量
    mark_num = 0
    # 最大标记数量
    max_mark = 3

    driver = None

    # 是否全部优化完成 若全部优化完成则可以不 switch_to 到 content_iframe 中
    all_optimized = False

    # 法人登录还是 自然人登录 法人登陆为 1 自然人登陆为 2
    login = 1
    # 是否在保存完成之后对表单内容进行验证
    verify = False

    # 对于 rd, ps 这些数据是否取 全部数据
    full_verify = False

    # TODO 这里需要根据登录账号的 col_id 修改
    co_id = None

    is_continue = False

    multi_recode_continue = False

    # TODO 申报材料年份
    year = 2020

    # is_test = False
    is_test = False

    # WriteIPFormRecord = []
    # WriteRDFormRecord = []
    WriteRdcostFormRecord = list()
    # WritePSFormRecord = []
    # IAStaDescRecord = []
    # WriteStandFormRecord = []

    # ent_res_status 状态信息
    status = None

    # entname 企业名称
    entname = ""

    # ent_flow_data 信息
    record = None

    logger = None

    db_con = 'online'

    record_num = 0
    # 暂存id
    ts_id = None

    # # # 录入完成的 id 列表
    completed_list = []
    # # # 录入失败的 id 列表
    error_list = []

    # # # 是否第一次录入
    is_first_entry = True
    # is_first_entry = False
    # 等待次数

    # 表单对应关系
    db_values = {}

    record_delete = False

    repetition = True

    withdrow = False

    models = []

    need_file = True

    title_loc = (By.CSS_SELECTOR, 'h2')
    next_btn_loc = (By.CSS_SELECTOR, ".pagination.pull-right li:nth-of-type(4) a")
    table_loc = (By.CSS_SELECTOR, ".tableRegionDivBody .tableBody")

    times = 3
    # 成立时间
    esdate = ""

    # 一次处理的公司数量
    CONTAINER_SIZE = 1000

    dict_cursor = pymysql.cursors.DictCursor

    hitech_pool = PooledDB(
        creator=pymysql,
        maxconnections=6,
        mincached=0,
        maxcached=5,
        maxshared=3,
        blocking=True,
        maxusage=None,
        setsession=[],
        ping=2,
        host=online_config.get("host"),
        port=online_config.get("port"),
        user=online_config.get("user"),
        password=online_config.get("password"),
        charset=online_config.get("charset"),
        database="hitech"
    )

    hzflow_pool = PooledDB(
        creator=pymysql,
        maxconnections=6,
        mincached=0,
        maxcached=5,
        maxshared=3,
        blocking=True,
        maxusage=None,
        setsession=[],
        ping=2,
        host=online_config.get("host"),
        port=online_config.get("port"),
        user=online_config.get("user"),
        password=online_config.get("password"),
        charset=online_config.get("charset"),
        database="hzflow"
    )

    @classmethod
    def put_db_values(cls):
        cls.db_values = {}
        for k, v in check_map.items():
            if v not in cls.db_values:
                cls.db_values[v] = dict()
            cls.db_values[v][k] = None


if __name__ == '__main__':
    g.put_db_values()
    print(g.db_values)

